var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/hooks/useDrsModel.ts
var useDrsModel_exports = {};
__export(useDrsModel_exports, {
  default: () => useDrsModel
});
module.exports = __toCommonJS(useDrsModel_exports);
var import_react = require("react");
var import_umi = require("umi");
var import_userInfo = require("../services/userInfo");
var import_useDrsPermission = __toESM(require("./helpers/useDrsPermission"));
function useDrsModel({ appCode }) {
  const [logining, setLogining] = (0, import_react.useState)(false);
  const [userInfo, setUserInfo] = (0, import_react.useState)();
  const [appList, setAppList] = (0, import_react.useState)([]);
  const gotoLoginPage = (0, import_react.useCallback)(async () => {
    import_umi.history.replace("/sso-pc/login");
  }, []);
  const clearUserInfo = (0, import_react.useCallback)(async () => {
    setUserInfo(void 0);
    gotoLoginPage();
  }, [gotoLoginPage]);
  (0, import_react.useEffect)(() => {
    window.addEventListener("tokenExpire", clearUserInfo);
    return () => {
      window.removeEventListener("tokenExpire", clearUserInfo);
    };
  }, [clearUserInfo]);
  const doGetUserInfo = (0, import_react.useCallback)(async () => {
    if (userInfo)
      return userInfo;
    setLogining(true);
    const res = await (0, import_userInfo.getUserInfo)({
      appCode
    });
    if ((res == null ? void 0 : res.success) && (res == null ? void 0 : res.data)) {
      const { appLoginUserDTOMap, ...rest } = res.data;
      const currentUserInfo = {
        ...rest,
        ...appLoginUserDTOMap[appCode]
      };
      setUserInfo(currentUserInfo);
      setLogining(false);
      return currentUserInfo;
    }
    setLogining(false);
  }, [userInfo, appCode]);
  const doGetTechnicalAppList = (0, import_react.useCallback)(async () => {
    const res = await (0, import_userInfo.technicalAppList)();
    if ((res == null ? void 0 : res.success) && (res == null ? void 0 : res.data)) {
      setAppList(res.data);
      return res.data;
    }
  }, [userInfo, appCode]);
  const doLogout = (0, import_react.useCallback)(async () => {
    const res = await (0, import_userInfo.logout)({
      appCode
    });
    if (res == null ? void 0 : res.success)
      clearUserInfo();
  }, [clearUserInfo, appCode]);
  const doChangeDept = (0, import_react.useCallback)(
    async (deptCode) => {
      var _a;
      setLogining(true);
      const res = await (0, import_userInfo.changeDept)({
        deptCode,
        appCode
      });
      setLogining(false);
      if ((res == null ? void 0 : res.success) && (res == null ? void 0 : res.data)) {
        const { appLoginUserDTOMap } = res.data;
        import_umi.history.replace(((_a = appLoginUserDTOMap == null ? void 0 : appLoginUserDTOMap[appCode]) == null ? void 0 : _a.defaultPageUrl) ?? "/");
        window.location.reload();
        return res.data;
      }
    },
    [userInfo, appCode]
  );
  const doChangeRole = (0, import_react.useCallback)(
    async (roleCode) => {
      var _a;
      setLogining(true);
      const res = await (0, import_userInfo.changeRole)({
        roleCode,
        appCode
      });
      setLogining(false);
      if ((res == null ? void 0 : res.success) && (res == null ? void 0 : res.data)) {
        const { appLoginUserDTOMap } = res.data;
        import_umi.history.replace(((_a = appLoginUserDTOMap == null ? void 0 : appLoginUserDTOMap[appCode]) == null ? void 0 : _a.defaultPageUrl) ?? "/");
        window.location.reload();
        return res.data;
      }
    },
    [userInfo, appCode]
  );
  const doChangeApp = (0, import_react.useCallback)(
    async (newAppCode) => {
      var _a;
      setLogining(true);
      const res = await (0, import_userInfo.changeApp)({
        appCode: newAppCode
      });
      setLogining(false);
      if ((res == null ? void 0 : res.success) && (res == null ? void 0 : res.data)) {
        const { appLoginUserDTOMap } = res.data;
        window.location.href = `/${newAppCode}/#${((_a = appLoginUserDTOMap == null ? void 0 : appLoginUserDTOMap[newAppCode]) == null ? void 0 : _a.defaultPageUrl) ?? "/"}`;
        return res.data;
      }
    },
    [userInfo, appCode]
  );
  const { canAccess, getBtnInfo } = (0, import_useDrsPermission.default)({ userInfo });
  return {
    /** 主应用标识，前端设置 */
    appCode,
    /** 登陆中 */
    logining,
    /** 已登录 */
    logined: userInfo && !!Object.keys(userInfo).length,
    /** 用户信息 */
    userInfo,
    /** 应用列表 */
    appList,
    /** 获取用户信息 */
    getUserInfo: doGetUserInfo,
    /** 获取应用列表 */
    getAppList: doGetTechnicalAppList,
    /** 更新用户信息 */
    setUserInfo,
    /** 清空用户信息 */
    clearUserInfo,
    /** 跳转到登录页面 */
    gotoLoginPage,
    /** 退出登录 */
    logout: doLogout,
    /** 切换部门 */
    changeDept: doChangeDept,
    /** 切换角色 */
    changeRole: doChangeRole,
    /** 切换应用 */
    changeApp: doChangeApp,
    /** 是否有权限访问 */
    canAccess,
    /** 获取按钮是否置灰等信息 */
    getBtnInfo,
    /** 子应用之间的跳转方式 */
    globalHistory: import_umi.history
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
